#!/usr/bin/env bash

#----------------------------------------
#       Name:                   install_vim.sh
#       Author:                 pokit@qq.com
#       Version:                1.0
#       Usage:                  install vim
#       Desc:                   wget https://gitee.com/banbaolatiao/shell_scripts/raw/master/install_neovim/install_neovim.sh
#
#----------------------------------------

#-----------------Declare----------------

set -e

#-----------------Function----------------

function fLogger() {
    if [ "$1"x = "red"x ]; then
        echo -e "\033[1;31m"$2"\033[0m"
    elif [ "$1" = "green" ]; then
        echo -e "\033[1;32m"$2"\033[0m"
    elif [ "$1" = "yellow" ]; then
        echo -e "\033[1;33m"$2"\033[0m"
    elif [ "$1" = "blue" ]; then
        echo -e "\033[1;34m"$2"\033[0m"
    else
        echo -e $*
    fi
}

function fParepareEnv() {
    fLogger blue "prepare install base env..."
    yum install -y epel-release gcc git wget ctags python3

    fLogger blue "prepare install pip3..."
    python3 -m pip install -U pip setuptools
    python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pynvim jedi

    fLogger blue "prepare install nodejs..."
    nodeVersion=v14.15.1
    yum install -y wget
    cd /usr/local/src
    wget https://npm.taobao.org/mirrors/node/${nodeVersion}/node-${nodeVersion}-linux-x64.tar.gz
    tar -zxvf node-${nodeVersion}-linux-x64.tar.gz
    rm -rf node-${nodeVersion}-linux-x64.tar.gz
    grep node /etc/profile >/dev/null && echo "node env already exist" || echo "export PATH="/usr/local/src/node-${nodeVersion}-linux-x64/bin:$PATH"" >>/etc/profile
    source /etc/profile
    npm config set registry https://registry.npm.taobao.org/
    npm install -g neovim
    npm install -g yarn

    fLogger blue "prepare install fzf..."
    git clone --depth 1 https://gitee.com/mirrors/fzf.git ~/.fzf
    ~/.fzf/install --all

    fLogger green "base env install success!"
}

function fInstallNvim() {
    fLogger blue "prepare install neovim..."
    wget -t3 https://github.com.cnpmjs.org/neovim/neovim/releases/download/v0.4.4/nvim.appimage -P /usr/local/
    cd /usr/local/
    chmod +x nvim.appimage
    ./nvim.appimage --appimage-extract
    ln -s /usr/local/squashfs-root/usr/bin/nvim /usr/local/bin/nvim
    ln -s /usr/local/squashfs-root/usr/bin/nvim /usr/local/bin/nv
    rm -rf nvim.appimage
    cd -
    fLogger green "install neovim success!"
}

function fInstallVimPlug() {
    fLogger blue "prepare install vim-plug..."
    curl --retry 3 -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs https://gitee.com/banbaolatiao/vim-plug/raw/master/plug.vim
    fLogger blue "prepare change vim-plug git source..."
    grep cnpm ~/.local/share/nvim/site/autoload/plug.vim >/dev/null && echo "already exist" || sed -i "s|github.com|github.com.cnpmjs.org|g" ~/.local/share/nvim/site/autoload/plug.vim
    fLogger green "install vim-plug success!"
}

function fConfigNvim() {
    fLogger blue "prepare download vim config..."
    curl --retry 3 -fLo ~/.config/nvim/init.vim --create-dirs https://gitee.com/banbaolatiao/vim-config/raw/master/init.vim

    fLogger blue "prepare install vim config..."
    nv +PlugInstall +qall
    nv +GoInstallBinaries +qall
}

function fRevomeAll() {
    rm -rf ~/.config/nvim
    rm -rf ~/.config/nvim/plugged
    rm -rf ~/.config/coc
    rm -rf ~/.config/configstore
    rm -rf ~/.config/plugged
    rm -rf ~/.local/share/nvim
}

# The command line help
function fDisplayHelp() {
    echo "Usage: $0 [option...]" >&2
    echo
    echo "   all              install all"
    echo "   config           config neovim"
    echo "   remove           remove all config"
    echo "   help             display help"
    echo
}

# Check if parameters options are given on the commandline
case "$1" in
all)
    fParepareEnv
    fInstallNvim
    fInstallVimPlug
    fConfigNvim
    ;;
config)
    fInstallVimPlug
    fConfigNvim
    ;;
remove)
    fRevomeAll
    ;;
-h | --help | help)
    fDisplayHelp
    ;;
*) # No more options
    fDisplayHelp
    exit 1
    ;;
esac
